package com.wdl.modules.myhouse.utils.util;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wdl.modules.myhouse.common.MyPage;
import com.wdl.modules.myhouse.vo.PageVO;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

@Slf4j
public class PageUtil {
    /**
     * 将PageVo 转化为 IPage
     */
    public static <T> IPage<T> pageVo2Ipage(PageVO pageVO, T t) {
        Page<T> page = new Page<>();
        try {
            page.setOrders(pageVO.getOrders());
            page.setCurrent(pageVO.getCurrentPage());
            page.setSize(pageVO.getPageSize());
            page.setPages((long) Math.ceil(pageVO.getTotal() / pageVO.getPageSize()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return page;
    }

    /**
     * pageVo 转 MyPage
     */
    public static MyPage pageVo2MyPage(PageVO vo) {
        MyPage page = new MyPage();
        try {
            page.setCurrent(vo.getCurrentPage());
            page.setSize(vo.getPageSize());
            page.setOrders(vo.getOrders());
            page.setRecords(vo.getTotal());
            page.setPageCount((int) Math.ceil(vo.getTotal() / vo.getPageSize()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return page;
    }

    /**
     * 构建分页对象
     *
     * @param
     * @param page 分页对象
     * @param <T>  泛型对象 new T();
     * @return
     */
    public static <T> IPage<T> getIPage(MyPage page, T t) {
        Page<T> tPage = new Page<>();
        if (page.getOrders() != null && page.getOrders().size() > 0) {
            List<OrderItem> orders = page.getOrders();
            for (OrderItem item : orders) {
                item.setColumn(UnderlineToCamelUtils.camelToUnderline(item.getColumn()));
            }
        }
        tPage.setCurrent(page.getCurrent());
        tPage.setSize(Long.valueOf(page.getSize()));
        tPage.setOrders(page.getOrders());
        log.info("工具类的page对象{}", tPage.getOrders());
        return tPage;
    }
}
